<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include 'errors.php';
class Jobseekers extends Errors {
	public function __construct(){
		parent::__construct();
		$this->output->nocache();
		$this -> form_validation -> set_error_delimiters('<p class="help-block danger">', '</p>');
	}
	public function index()
	{
		if ($this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/welcome");
		}
		if($this -> session -> userdata('employer_active_access')){
			$this->session->unset_userdata('employer_active_access');
		}
		$data['title']="Jobseekers";
		$data['locations']=$this->model_globals->select_all('job_locations');
		$categories=$this->model_globals->select_all('job_categories')->result();
		$option = $this->get_job_category_options($categories);
		$data['categories']=$option;
		//$data['user_info']=$this->model_globals->select_where("job_seekers",array("job_seeker_id"=>$this -> session -> userdata('jobseeker_active_access')))->result;
		$this->load->view(F_MASTER."index",$data);

	}
	public function profile()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['username']=$this->get_name_jobseeker();
		$data['title']="My Profile";
		
		$locations=$this->model_globals->select_join('job_preference_locations', array('job_locations' => array('using_location_id' => 'job_location_id')),'inner',array('using_job_seeker_id' => $this->session->userdata('jobseeker_active_access')));
		//$data['locations']=$this->model_globals->select_all('job_locations');
		$option_location="";
		foreach($locations->result() as $location){
			$location_id=$location->job_location_id;
			$name=$location->name;
			$is_prefered=($location->is_prefered=="YES") ?"checked":'';
			$option_location.='<div class="col-md-4 clear"><input type="checkbox" name="locations[]" value="'.$location_id.'"'.$is_prefered.' />&nbsp;'. $name.'</a></div>';
							
		}
		
		$data['locations']=$option_location;		
		$data['briefly_desc']=$this->model_globals->select_where('job_seekers_job_preference',array('using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));	
		
		$categories=$this->model_globals->select_all('job_categories')->result();
		$option = $this->get_job_category_options($categories);
			
		$data['categories']=$option;
		$data['browse_locations']=$this->model_globals->select_all('job_locations');
		$data['browse_categories']=$this->model_globals->select_all('job_categories');
		$data['user_info']=$this->model_globals->select_where("job_seekers",array("job_seeker_id"=>$this -> session -> userdata('jobseeker_active_access')));
		$this->load->view(F_MASTER."index",$data);
	}
	public function welcome()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		
		$data['username']=$this->get_name_jobseeker();
		$data['title']="Jobseekers";
		$data['locations']=$this->model_globals->select_all('job_locations');
		$categories=$this->model_globals->select_all('job_categories')->result();
		$option = $this->get_job_category_options($categories);
		$this->load->view(F_MASTER."index",$data);
	}
	public function login()
	{
		$data['title']="Jobseeker Login";
		if ($this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/welcome");
		} else {
			$this -> form_validation -> set_rules('email', 'Email', 'required|valid_email');
			$this -> form_validation -> set_rules('password', 'Password', 'required');
			//$this -> form_validation -> set_error_delimiters('<div class="alert alert-danger" style="position:absolute;top:20px;text-align:center; width:92%;">', '</div>');

			if ($this -> form_validation -> run() == FALSE) {
				$data['locations']=$this->model_globals->select_all('job_locations');
				$data['feature_jobs']=$this->model_jobs->fearture_job();
				$this->load->view(F_MASTER."index",$data);
			} else {
				$data_logged=array(
					'email'=>$this->input->post('email'),
					'password'=>sha1($this->input->post('password'))
				);
				
				$login_check=$this->model_globals->select_where('job_seekers',$data_logged);
				if($login_check->num_rows==1){
					
					$status=$login_check->row()->status;
					if($status!=1){
						$this->session->set_flashdata('warning',"Your account was not activated yet.Please check you email to activation account or click link here to <a href='".base_url()."jobseekers/resent-activation-account'>Resent your activation account.</a>");
						redirect('jobseekers/login');
					}else{
						if($this -> session -> userdata('employer_active_access')){
							$this->session->unset_userdata('employer_active_access');
						}
						$this -> session -> set_userdata('jobseeker_active_access', $login_check ->row()->job_seeker_id);
						
						// request login before apply job
						if($this->session->userdata('request_login_before_apply_job')){
							$job_keys=$this->session->userdata('request_login_before_apply_job');
							
							redirect('jobs/detail/'.$job_keys);
						}
						
						// redirect to profile
						redirect('jobseekers/welcome');	
					}
					
					
				}else{
					$this -> session -> set_flashdata('error_login', TRUE);
					redirect('jobseekers/login');
				}
			}
		}
		
	}
	public function register()
	{
		$data['title']="Jobseeker Registration";
		
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[job_seekers.email]');
		$this->form_validation->set_rules('first_name', 'First Name', 'required|min_length[2]|max_length[25]');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required|min_length[2]|max_length[25]');
		$this -> form_validation -> set_rules('title', 'Title', 'required');		
		$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]|min_length[6]|max_length[50]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required|min_length[6]|max_length[50]');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['locations']=$this->model_globals->select_all('job_locations');
			$categories=$this->model_globals->select_all('job_categories')->result();
			$option = $this->get_job_category_options($categories);
			$data['feature_jobs']=$this->model_jobs->fearture_job();
			$this->load->view(F_MASTER."index",$data);
		} else {
			$token_key=random_string('alnum', 25);
			$picture="user.jpg";
			if($this->input->post('subscribe',TRUE)==1){
				$subscribe="YES";
			}else{
				$subscribe="NO";
			}
			$birth = explode("/",$this->input->post('dob',TRUE));
			$date_of_birth = $birth[2] . '-' . $birth[1] . '-' . $birth[0];
			$data=array(
				'email'=>$this->input->post('email',TRUE),
				'password'=>sha1($this->input->post('password',TRUE)),
				'title'=>$this->input->post('title',TRUE),
				'first_name'=>$this->input->post('first_name',TRUE),
				'last_name'=>$this->input->post('last_name',TRUE),
				'address'=>$this->input->post('address',TRUE),
				'phone'=>$this->input->post('phone',TRUE),
				'mobile'=>$this->input->post('mobile',TRUE),
				'date_of_birth'=>$date_of_birth,
				'gender'=>$this->input->post('gender',TRUE),
				'picture'=>$picture,
				'subscribe'=>$subscribe,
				'token_key'=>$token_key,
				'status'=>0,
				'created_date'=>null
			);
			
			$job_seeker_id=$this->model_globals->insert_get_last_id('job_seekers',$data);
			if($job_seeker_id){
				//insert preference
				$this->brief_data($job_seeker_id);
				
				//sent activation link
				//http:www.jobpending.com/employers/activation-account/ICiiwwpeurnfdksivnEjueT
					//$activation_key=random_string('alnum', 25);
					$email=$this->input->post('email');
					
					$is_send_activate_account=$this->send_mail_to_jobseeker_activation_account($token_key,$email);
					if($is_send_activate_account){
						  $this -> session -> set_flashdata('success', "You have successfully created an account.An email has been sent to < $email > containing the instructions to verify your account.Please check your email.");
						redirect('jobseekers/login');
					}else{
						$this -> session -> set_flashdata('warning', 'You have successfully created an account.but An email has been sent to < $email > containing the instructions to verify your account is fail.Please help us to click on this link to resent your activation account.Thank you !');
						redirect('jobseekers/resent-activation-account');
					}
			}else{
				$this -> session -> set_flashdata('error', 'Your account  was registered fail !.');
				redirect('jobseekers/register');
			}
		}
		
	}
	public function forgotten()
	{
		$data['title']="Jobseeker Forgotten Password";
		$this->form_validation->set_rules('email','Email','required|valid_email');
		if($this->form_validation->run()===FALSE){
			$data['feature_jobs']=$this->model_jobs->fearture_job();
			$data['locations']=$this->model_globals->select_all('job_locations');
			$categories=$this->model_globals->select_all('job_categories')->result();
			$option = $this->get_job_category_options($categories);
			$data['categories']=$option;
			$this->load->view(F_MASTER."index",$data);
		}else{
			$email=$this->input->post('email');	
			$checking_email=$this->model_globals->select_where('job_seekers',array('email'=>$email));
			if($checking_email->num_rows()==1){
				$first_name=$checking_email->row()->first_name;
				$last_name=$checking_email->row()->last_name;
				$token_key=random_string('alnum', 25);
				$update_token_key=$this->model_globals->update('job_seekers',array('token_key'=>$token_key),array('email'=>$email));
				if($update_token_key){
					//send mail to user with activation link reset new password
					//http:www.jobpending.com/jobseekers/activation_forgotten_password/ekeii#EwiTjjwjwwk
					//$key_active_password=random_string('alnum', 25);
					$is_sent_reset_password=$this->send_mail_to_jobseeker_reset_password($token_key,$email);
					if($is_sent_reset_password){
						$this->session->set_flashdata('success','Your request to reset new password was confirmed.Please checking your email to reset new password.');
						redirect('jobseekers/forgotten');
					}else{
						$this->session->set_flashdata('error','Your request to reset new password was confirmed please.Please try again.');
						redirect('jobseekers/forgotten');
					}
					
				}else{
					$this->session->set_flashdata('error','There is has error occured. Please try again !');
					redirect('jobseekers/forgotten');
				}
			}else{
				$this->session->set_flashdata('error','Your email that you have provided is wrong !');
				redirect('jobseekers/forgotten');
			}
		}
		
	}
	public function resent_activation_account()
	{
		$data['title']="Resent Activation An Account";
		$this->form_validation->set_rules('email','Email','required|valid_email');
		if($this->form_validation->run()===FALSE){
			$data['feature_jobs']=$this->model_jobs->fearture_job();
			$data['locations']=$this->model_globals->select_all('job_locations');
			$categories=$this->model_globals->select_all('job_categories')->result();
			$option = $this->get_job_category_options($categories);
			$data['categories']=$option;
			$this->load->view(F_MASTER."index",$data);
		}else{
			$email=$this->input->post('email');	
			$checking_email=$this->model_globals->select_where('job_seekers',array('email'=>$email));
			if($checking_email->num_rows()==1){
				$first_name=$checking_email->row()->first_name;
				$last_name=$checking_email->row()->last_name;
				$token_key=random_string('alnum', 25);
				$update_token_key=$this->model_globals->update('job_seekers',array('token_key'=>$token_key),array('email'=>$email));
				if($update_token_key){
					//send mail to user with activation link reset new password
					//http:www.jobpending.com/jobseekers/activation_forgotten_password/ekeii#EwiTjjwjwwk
					//$activation_key=random_string('alnum', 25);
					$is_sent_resent_activate=$this->send_mail_to_jobseeker_resend_activate_account($token_key,$email);
					if($is_sent_resent_activate){
						$this -> session -> set_flashdata('success', 'Your request to resent activation account success!.Please checking your email to reset new password.');
						redirect('jobseekers/resent-activation-account');
					}else{
						$this->session->set_flashdata('error','Your request to resent activation account fail!.Please try again.');
						redirect('jobseekers/resent-activation-account');
					}
					
				}else{
					$this->session->set_flashdata('error','There is has error occured. Please try again !');
					redirect('jobseekers/resent-activation-account');
				}
			}else{
				$this->session->set_flashdata('error','Your email that you have provided is wrong !');
				redirect('jobseekers/resent-activation-account');
			}
		}
		
	}
	public function activation_account()
	{
		$data['title']="Jobseeker Activation An Account";
		$token_key=$this->uri->segment(3);
		$checking_actavate_key=$this->model_globals->select_where("job_seekers",array('token_key'=>$token_key));

		if($checking_actavate_key->num_rows()>0){
			$set_active_account=$this->model_globals->update('job_seekers',array('status'=>1),array('token_key'=>$token_key));
			if($set_active_account){
				$this->session->set_flashdata('success','Your account was actived success.Please login to your account !');
				redirect("jobseekers/login");
			}else{
				$data['feature_jobs']=$this->model_jobs->fearture_job();
				$data['wrong_activation']='There is an error occured.Please try again !';
				$this->load->view(F_MASTER."index",$data);
			}
		}else{
		
			$data['feature_jobs']=$this->model_jobs->fearture_job();
			$data['wrong_activation']="Your activation link is wrong. Please try again !";
			$this->load->view(F_MASTER."index",$data);
				
		}
		
	}
	public function activation_forgotten_password(){
		$data['title']="Jobseeker Activation Forgotten Password";
		$token_key=$this->uri->segment(3);
		$checking_actavate_key=$this->model_globals->select_where("job_seekers",array('token_key'=>$token_key));
		if($checking_actavate_key->num_rows()>0){
			$this->session->set_userdata('activation_password',$checking_actavate_key->row()->job_seeker_id);
			redirect('jobseekers/reset-password');
		}else{
			$data['feature_jobs']=$this->model_jobs->fearture_job();	
			$data['wrong_activation']="Your activation link is wrong. Please try again !";
			$this->load->view(F_MASTER."index",$data);
		}
	}
	public function reset_password()
	{
		$data['title']="Jobseeker Reset Password";
		if($this->session->userdata('activation_password')){
			$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]|min_length[6]|max_length[50]');
			$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required|min_length[6]|max_length[50]');
				
			if ($this -> form_validation -> run() === FALSE) {
				$data['feature_jobs']=$this->model_jobs->fearture_job();
				$this->load->view(F_MASTER."index",$data);
			} else {
				$data=array(
					'password'=>sha1($this->input->post('password',TRUE))
				);
				$is_updated=$this->model_globals->update("job_seekers",$data,array("job_seeker_id"=>$this->session->userdata('activation_password')));
				if($is_updated){
					$this->session->unset_userdata('activation_password');
					$this -> session -> set_flashdata('success', "You have reseted your password success !");
					redirect('jobseekers/login');
				}else{
					$this -> session -> set_flashdata('error',"You have reseted your password fail !");
					redirect('jobseekers/login');
				}
			}
		}else{
			redirect("jobseekers/login");
		}
	}
	public function edit_profile()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Edit Profile";
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
		$this->form_validation->set_rules('first_name', 'First Name', 'required|min_length[2]|max_length[25]');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required|min_length[2]|max_length[25]');
		$this -> form_validation -> set_rules('title', 'Title', 'required');		
		
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['locations']=$this->model_globals->select_all('job_locations');
			$categories=$this->model_globals->select_all('job_categories')->result();
			$option = $this->get_job_category_options($categories);
			$data['feature_jobs']=$this->model_jobs->fearture_job();
			$data['username']=$this->get_name_jobseeker();
			$data['user_info']=$this->model_globals->select_where("job_seekers",array("job_seeker_id"=>$this -> session -> userdata('jobseeker_active_access')));
			$this->load->view(F_MASTER."index",$data);
			
		} else {
			
			$birth = explode("/",$this->input->post('dob',TRUE));
			$date_of_birth = $birth[2] . '-' . $birth[1] . '-' . $birth[0];
			
			$data=array(
				'email'=>$this->input->post('email',TRUE),
				'title'=>$this->input->post('title',TRUE),
				'first_name'=>$this->input->post('first_name',TRUE),
				'last_name'=>$this->input->post('last_name',TRUE),
				'address'=>$this->input->post('address',TRUE),
				'phone'=>$this->input->post('phone',TRUE),
				'mobile'=>$this->input->post('mobile',TRUE),
				'date_of_birth'=>$date_of_birth,
				'gender'=>$this->input->post('gender',TRUE)
			);
			
			$is_updated=$this->model_globals->update('job_seekers',$data,array("job_seeker_id"=>$this -> session -> userdata('jobseeker_active_access')));
			if($is_updated){
				
				$this -> session -> set_flashdata('success', "Your profile have been updated successfully");
				redirect('jobseekers/edit-profile');
					
			}else{
				$this -> session -> set_flashdata('error', 'Your profile have been updated fail.');
				redirect('jobseekers/edit-profile');
			}
		}
		
	}
	public function messages(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Message";
		$data['username']=$this->get_name_jobseeker();
		$this->load->view(F_MASTER."index",$data);
	}
	public function change_password()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Change Password";
		$data['username']=$this->get_name_jobseeker();
		$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]|min_length[6]|max_length[50]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required|min_length[6]|max_length[50]');
			
		if ($this -> form_validation -> run() === FALSE) {
			$this->load->view(F_MASTER."index",$data);
		} else {
			$data=array(
				'password'=>sha1($this->input->post('password',TRUE))
			);
			
			$is_updated=$this->model_globals->update("job_seekers",$data,array("job_seeker_id"=>$this->session->userdata('jobseeker_active_access')));
			if($is_updated){
				$this -> session -> set_flashdata('success', "You have changed your password success !");
				redirect('jobseekers/welcome');
			}else{
				$this -> session -> set_flashdata('error',"You have changed your password fail !");
				redirect('jobseekers/welcome');
			}
		}
	}
	
	public function modify_cv()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Edit CV";
		$this->load->view(F_MASTER."index",$data);
	}
	public function created_cv()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Create CV";
		$this->load->view(F_MASTER."index",$data);
	}
	public function received_messages()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Received Message";
		$this->load->view(F_MASTER."index",$data);
	}
	public function job_preferences()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Job Preferences";
		$job_seeker_id=$this->session->userdata('jobseeker_active_access');
		$data['username']=$this->get_name_jobseeker();
		if(!$this->input->post('job_preferences')){
			$locations=$this->model_globals->select_join('job_preference_locations', array('job_locations' => array('using_location_id' => 'job_location_id')),'inner',array('using_job_seeker_id' => $this->session->userdata('jobseeker_active_access')));
		//$data['locations']=$this->model_globals->select_all('job_locations');
		$option_location="";
		foreach($locations->result() as $location){
			$location_id=$location->job_location_id;
			$name=$location->name;
			$is_prefered=($location->is_prefered=="YES") ?"checked":'';
			$option_location.='<div class="col-md-4 clear"><input type="checkbox" name="locations[]" value="'.$location_id.'"'.$is_prefered.' />&nbsp;'. $name.'</a></div>';
							
		}
		
		$data['locations']=$option_location;		
		$data['briefly_desc']=$this->model_globals->select_where('job_seekers_job_preference',array('using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));	
		
		$categories=$this->model_globals->select_all('job_categories')->result();
			$option="";
			foreach($categories as  $category){
				$category_id=$category->job_category_id;
				$name=$category->name;
				$option.='<div class="col-md-12 clear"><b>'.$name.'</b></div>';
				//$sub_categories=$this->model_globals->select_where('job_sub_categories',array('using_job_category_id'=>$category_id))->result();
				$sub_categories=$this->model_globals->select_join('job_preference_categories', array('job_sub_categories' => array('using_job_sub_category_id' => 'job_sub_category_id')),'inner',array('job_preference_categories.using_job_category_id'=>$category_id,'using_job_seeker_id' => $this->session->userdata('jobseeker_active_access')))->result();
				foreach($sub_categories as $sub_category){
					$sub_category_id=$sub_category->job_sub_category_id;
					$sub_name=$sub_category->name;
					$is_prefered=($sub_category->is_prefered=="YES") ?"checked":'';
					
					$option.='<div class="col-md-4 clear"><input type="checkbox" name="categories[]" value="'.$sub_category_id.'"'.$is_prefered.'/>&nbsp;'.$sub_name.'</div>';
					//$option.='<option value="'.$category_id.'-'.$sub_category_id.'">'.$sub_name.'</option>';
				}
			}
			
		$data['categories']=$option;
			
		$data['categories']=$option;
			$this->load->view(F_MASTER."index",$data);	
		}else{
			$briefly_description=$this->input->post('briefly_description');
			$prefer_locations=$this->input->post('locations');
			$prefer_categories=$this->input->post('categories');
			
			$this->model_globals->update('job_seekers_job_preference',array('briefly_description'=>$briefly_description),array('using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
			$this->load->model('F-MODEL/model_job_seekers');
			
			$this->model_job_seekers->update_prefer_locations($prefer_locations,$job_seeker_id);
			$this->model_job_seekers->update_not_prefer_locations($prefer_locations,$job_seeker_id);
			
			$this->model_job_seekers->update_prefer_categories($prefer_categories,$job_seeker_id);
			$this->model_job_seekers->update_not_prefer_categories($prefer_categories,$job_seeker_id);
			redirect('jobseekers/profile');
		}
		
	}
	public function job_alerts()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['username']=$this->get_name_jobseeker();
		$data['title']="Job Alert";
		$data['locations']=$this->model_globals->select_all('job_locations');
		$categories=$this->model_globals->select_all('job_categories')->result();
		$option = $this->get_job_category_options($categories);
		$data['categories']=$option;
		$data['job_alerts']=$this->model_globals->select_join_get_only_fields('job_alerts', $arr_fields = array('job_alert_id','job_locations.name AS location_name','job_sub_categories.name AS category_name	','keyword'),array('job_locations' => array('using_job_location_id' => 'job_location_id'),'job_sub_categories' => array('using_job_sub_category_id' => 'job_sub_category_id')), $join_type = 'INNER', $arr_where = array('using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')), $limit = NULL,$sort="job_alert_id DESC");
		$this->load->view(F_MASTER."index",$data);
	}
	public function new_rule(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="New Rule";
		$data['username']=$this->get_name_jobseeker();
		$this->form_validation->set_rules('category', 'Job Category', 'required');
		$this->form_validation->set_rules('location', 'Location', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['locations']=$this->model_globals->select_all('job_locations');
			$categories=$this->model_globals->select_all('job_categories')->result();
			$option = $this->get_job_category_options($categories);
			$data['categories']=$option;
			
			$this->load->view(F_MASTER."index",$data);
		} else {
			$splite_category=explode("-",$this->input->post('category'));
			$data=array(
				'using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access'),
				'keyword'=>$this->input->post('keyword'),
				'using_job_category_id'=>$splite_category[0],
				'using_job_sub_category_id'=>$splite_category[1],
				'using_job_location_id'=>$this->input->post('location'),
				'status'=>1,
				'created_date'=>null
			);
			
			$is_inserted=$this->model_globals->insert('job_alerts',$data);
			if($is_inserted){
				$this -> session -> set_flashdata('success', 'New rule  was created success !.');
				redirect('jobseekers/job-alerts');
			}else{
				$this -> session -> set_flashdata('error', 'New rule  was created fail !.');
				redirect('jobseekers/job-alerts');
			}
		}
	}
	public function edit_rule(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Edit Rule";
		$rule_id=$this->uri->segment(3);
		$data['username']=$this->get_name_jobseeker();
		$this->form_validation->set_rules('category', 'Job Category', 'required');
		$this->form_validation->set_rules('location', 'Location', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['locations']=$this->model_globals->select_all('job_locations');
			$categories=$this->model_globals->select_all('job_categories')->result();
			$option = $this->get_job_category_options($categories);
			$data['categories']=$option;
			$data['rule_info']=$this->model_globals->select_where('job_alerts',array('job_alert_id'=>$rule_id));
			if($data['rule_info']->num_rows()!=1){
				$this->error_404();
			}else{
				$this->load->view(F_MASTER."index",$data);	
			}
			
		} else {
			$splite_category=explode("-",$this->input->post('category'));
			$data=array(
				'using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access'),
				'keyword'=>$this->input->post('keyword'),
				'using_job_category_id'=>$splite_category[0],
				'using_job_sub_category_id'=>$splite_category[1],
				'using_job_location_id'=>$this->input->post('location')
			);
			
			$is_updated=$this->model_globals->update('job_alerts',$data,array('job_alert_id'=>$rule_id,'using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
			if($is_updated){
				$this -> session -> set_flashdata('success', 'You have edited your rule success !.');
				redirect('jobseekers/job-alerts');
			}else{
				$this -> session -> set_flashdata('success', 'You have edited your rule fail !.');
				redirect('jobseekers/job-alerts');
			}
		}
	}
	public function delete_rule(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$rule_id=$this->uri->segment(3);
		$is_deleted=$this->model_globals->delete('job_alerts',array('job_alert_id'=>$rule_id,'using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Your rule was deleted success !.');
			redirect('jobseekers/job-alerts');
		}else{
			$this -> session -> set_flashdata('error', 'Your rule  was deleted fail !.');
			redirect('jobseekers/job-alerts');
		}
	}
	public function documents()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['username']=$this->get_name_jobseeker();
		$data['title']="Documents";
		$data['locations']=$this->model_globals->select_all('job_locations');
		$categories=$this->model_globals->select_all('job_categories')->result();
		$option = $this->get_job_category_options($categories);
		$data['my_documents']=$this->model_globals->select_where('seeker_documents',array('using_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
		$this->load->view(F_MASTER."index",$data);
	}
	public function new_document(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="New Document";
		$this->form_validation->set_rules('resume', 'Resume', 'required');
		//$this->form_validation->set_rules('userfile', 'Choose file', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$this->load->view(F_MASTER."index",$data);
		} else {
			 $config['upload_path'] = './Templates/F-Template/F-DOCS/seeker_documents/';
           	$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|doc|docx';
            $config['max_size'] = '30000';
            $this->load->library('upload', $config);

            if (!$this->upload->do_upload()) {
            	$data['errors']= array('error' => $this->upload->display_errors());
                $this->load->view(F_MASTER."index",$data);
            } else {

                $data = $this->upload->data();
                $file_name = $data['file_name'];
				$data=array(
					'is_resume'=>$this->input->post('resume',TRUE),
					'description'=>$this->input->post('desc',TRUE),
					'file_name'=>$file_name,
					'file_type'=>$data['file_type'],
					'file_size'=>$data['file_size'],
					'using_seeker_id'=>$this->session->userdata('jobseeker_active_access'),
					'created_date'=>null
				);
				
				$is_inserted=$this->model_globals->insert("seeker_documents",$data);
				if($is_inserted){
					$this -> session -> set_flashdata('success', "You have added new document success !");
					redirect('jobseekers/documents');
				}else{
					$this -> session -> set_flashdata('error', "You have added new document fail !");
					redirect('jobseekers/documents');
				}
             }
		}
	}
	public function my_document(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="My Documents";
		$data['username']=$this->get_name_jobseeker();
		$data['my_documents']=$this->model_globals->select_where('seeker_documents',array('using_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
		$this->load->view(F_MASTER."index",$data);
	}
	public function download_document(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$doc_id=$this->uri->segment(3);
		$file_name=$this->model_globals->select_string('seeker_documents','file_name',array('seeker_document_id'=>$doc_id));
		if($file_name =="" || $file_name==null){
			$this->error_404();
		}else{
			$this->load->helper('download');
			$data = file_get_contents("./Templates/F-Template/F-DOCS/seeker_documents/".$file_name); // Read the file's contents
			//$name = 'myphoto.jpg';
	
			force_download($file_name, $data);
		}
		
		
	}
	public function modify_document(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Modify Document";
		
		$doc_id=$this->uri->segment(3);
		$data['username']=$this->get_name_jobseeker();
		$this->form_validation->set_rules('resume', 'Resume', 'required');
		//$this->form_validation->set_rules('userfile', 'Choose file', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['doc_info']=$this->model_globals->select_where('seeker_documents',array('seeker_document_id'=>$doc_id));
			if($data['doc_info']->num_rows!=1){
				$this->error_404();
			}else{
				$this->load->view(F_MASTER."index",$data);	
			}
			
		} else {
			$file_name="";
			$error_upload=FALSE;
			if (!empty($_FILES['userfile']['name'])){
				$config['upload_path'] = './Templates/F-Template/F-DOCS/seeker_documents/';
	           	$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|doc|docx';
	            $config['max_size'] = '30000';
	            $this->load->library('upload', $config);
	
	            if (!$this->upload->do_upload()) {
	            	$data['errors']= array('error' => $this->upload->display_errors());
					$error_upload=TRUE;
	                $this->load->view(F_MASTER."index",$data);
	            } else {
	
	                $data = $this->upload->data();
	                $file_name = $data['file_name'];
				}
			}
			
			if($file_name!=""){
				$data=array(
					'is_resume'=>$this->input->post('resume',TRUE),
					'description'=>$this->input->post('desc',TRUE),
					'file_name'=>$file_name,
					'file_type'=>$data['file_type'],
					'file_size'=>$data['file_size'],
					'using_seeker_id'=>$this->session->userdata('jobseeker_active_access')
				);	
			}else{
				$data=array(
					'is_resume'=>$this->input->post('resume',TRUE),
					'description'=>$this->input->post('desc',TRUE),
					'using_seeker_id'=>$this->session->userdata('jobseeker_active_access')
				);	
			}
				
			if($error_upload!=TRUE){
				$is_updated=$this->model_globals->update("seeker_documents",$data,array('seeker_document_id'=>$doc_id));
				if($is_updated){
					$this -> session -> set_flashdata('success', "You have modified document success !");
					redirect('jobseekers/documents');
				}else{
					$this -> session -> set_flashdata('error', "You have modified document fail !");
					redirect('jobseekers/documents');
				}
			}
				
		}
	}
	
	public function delete_document(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$doc_id=$this->uri->segment(3);
		$is_deleted=$this->model_globals->delete('seeker_documents',array('seeker_document_id'=>$doc_id));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Your document was deleted success !.');
			redirect('jobseekers/documents');
		}else{
			$this -> session -> set_flashdata('error', 'Your document  was deleted fail !.');
			redirect('jobseekers/documents');
		}
	}
    public function is_isset_resume($authorId){
        $result = $this -> model_globals->select_join('resume', array('job_seeker_proficiency_language' => array('resume_id' => 'using_resume_id')),'inner',array('using_job_seeker_id' => $authorId),'1');
        if($result->num_rows() > 0){
            return TRUE;
        }else{
            return FALSE;
        }
    }
	public function resume()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
        if($this->is_isset_resume($this -> session -> userdata('jobseeker_active_access'))){
            $data['cv'] = TRUE;
        }else{
            $data['cv'] = FALSE;
        }
		if($this->model_globals->check_data_exist('resume_text_editors',array('using_job_seeker_id'=>$this -> session -> userdata('jobseeker_active_access')))){
            $data['cv_editor'] = TRUE;
        }else{
            $data['cv_editor'] = FALSE;
        }
		$data['username']=$this->get_name_jobseeker();
		$data['title']="Resume";
		$data['locations']=$this->model_globals->select_all('job_locations');
		$categories=$this->model_globals->select_all('job_categories')->result();
		$option = $this->get_job_category_options($categories);
		$this->load->view(F_MASTER."index",$data);
	}
	public function resume_creator(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		if($this->is_isset_resume($this -> session -> userdata('jobseeker_active_access'))){
			
            $data['cv'] = TRUE;
        }else{
            $data['cv'] = FALSE;
        }
        $this->form_validation->set_rules('school_name','Schoool name','required');
        $this->form_validation->set_rules('education_level','Education Level','required');
        $this->form_validation->set_rules('program_name','Program name','required');
        $this->form_validation->set_rules('start_edu_date','Date attended','required');
        $this->form_validation->set_rules('end_edu_date','Date graduated','required');
        
        $this->form_validation->set_rules('job_title','Job title','required');
        $this->form_validation->set_rules('copany_name','Company name','required');
        $this->form_validation->set_rules('employer_name','Employer name','required');
        $this->form_validation->set_rules('employer_address','Employer address','required');
        $this->form_validation->set_rules('job_duty','Job duty','required');
        $this->form_validation->set_rules('start_exp_date','Start date of experience','required');
        $this->form_validation->set_rules('end_pex_date','End date of experience','required');
        
        $this->form_validation->set_rules('navtive_language','Native language','required');
        $this->form_validation->set_rules('language','Language','required');
        $this->form_validation->set_rules('level','Language level','required');
        
        $this->form_validation->set_rules('skill','Skill name','required');
        $this->form_validation->set_rules('interest','Interest','required');
        $this->form_validation->set_rules('reference','Reference','required');
        
        if($this->form_validation->run()!=TRUE){
            $data['username']=$this->get_name_jobseeker();
    		$data['title']="Resume Creator";
    		$data['locations']=$this->model_globals->select_all('job_locations');
    		$categories=$this->model_globals->select_all('job_categories')->result();
    		$option = $this->get_job_category_options($categories);
            $data['laguags'] = $this->model_globals->select_all('languages',150);
            $data['language_level']= $this->model_globals->select_all('proficiency_languages',150);
            $data['education_level']= $this->model_globals->select_all('education_levels',30);
              
    		$this->load->view(F_MASTER."index",$data);
        }else{
            $data_native_language = array(
                'using_job_seeker_id' => $this -> session -> userdata('jobseeker_active_access'),
                'skill' => $this->input->post('skill',TRUE),
                'native_language_using_language_id' =>  $this->input->post('navtive_language',TRUE),
                'interesting' => $this->input->post('interest',TRUE),
                'video_resume' => "",
                'reference' => $this->input->post('reference',TRUE),
                'created_date' => null,
                'status' => TRUE
            );
            $result = FALSE;
            $resumId = $this->model_globals->insert_get_last_id('resume',$data_native_language); /*Insert native language & return last resume id*/
            $language = $this->input->post('language',TRUE);
            $language_level = $this->input->post('level',TRUE);
            for($i=0; $i<count($language); $i++){
                  $result = TRUE;
                  /*Insert proficiency languae as an array*/
                  $this->model_globals->insert('job_seeker_proficiency_language',array('using_resume_id'=>$resumId,'using_language_id'=>$language[$i],'using_proficiency_language_id'=>$language_level[$i],'created_date'=>NULL,'status'=>TRUE));  
                
            }
            $education_level = $this->input->post('education_level',TRUE);
            $school_name = $this->input->post('school_name',TRUE) ;
            $program_name = $this->input->post('program_name',TRUE);
		
            $edu_start_date = $this->input->post('start_edu_date',TRUE);
			$edu_end_date = $this->input->post('end_edu_date',TRUE);
			
			
            for($i=0; $i<count($education_level); $i++){
            	
			$date_start_input = explode("/", $edu_start_date[$i]);
			$date_start_db = $date_start_input[2] . "-" . $date_start_input[1] . "-" . $date_start_input[0];
			
			$date_end_input = explode("/", $edu_end_date[$i]);
			$date_end_db = $date_end_input[2] . "-" . $date_end_input[1] . "-" . $date_end_input[0];

                $result = TRUE;
                /*Insert seeker's education as an array'*/
                $this->model_globals->insert('job_seeker_educations',array('using_resume_id'=>$resumId,'using_education_level_id'=>$education_level[$i],'school_name'=>$school_name[$i],'program_name'=>$program_name[$i],'date_attended'=>$date_start_db,'date_gratuated'=>$date_end_db,'created_date'=>NULL,'status'=>TRUE));
                          
            }
            $job_title = $this->input->post('job_title',TRUE);
            $company_name = $this->input->post('copany_name',TRUE);
            $employer_name = $this->input->post('employer_name',TRUE);
            $employer_address = $this->input->post('employer_address',TRUE);
            $job_duty = $this->input->post('job_duty',TRUE);
            $start_expDate = $this->input->post('start_exp_date',TRUE);
            $end_expDate = $this->input->post('end_pex_date',TRUE);
            for($i=0; $i<count($job_title); $i++){
                $result = TRUE;
                /*Insert seeker's work history as an array*/
                $this->model_globals->insert('work_history',array('using_resume_id'=>$resumId,'job_title'=>$job_title[$i],'company_name'=>$company_name[$i],'employer_name'=>$employer_name[$i],'employer_address'=>$employer_address[$i],'job_duties'=>$job_duty[$i],'start_date_end_date'=>$start_expDate[$i].'-'.$end_expDate[$i],'created_date'=>NULL,'status'=>TRUE));                   
            }
            if($result != FALSE){
                $this->session->set_flashdata('success','Your resume has been successful created!');
            }else{
                $this->session->set_flashdata('error','Your resume has not been successful created!');
            }
            redirect('jobseekers/resume');
            
        }	
	}
    public function modify_resume_creator(){
        if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		if($this->is_isset_resume($this -> session -> userdata('jobseeker_active_access'))){
            $data['cv'] = TRUE;
        }else{
            redirect('jobseekers/resume-creator');
        }
		$data['title']="Modify Resume Creator";
		$data['username']=$this->get_name_jobseeker();
		
        $seekerId = $this->session->userdata('jobseeker_active_access');
        $get_resumId = $this->model_globals->select_string('resume','resume_id',array('using_job_seeker_id'=>$seekerId));
        $data['education'] = $this->model_globals->select_where('job_seeker_educations',array('using_resume_id'=> $get_resumId));
        $data['history'] = $this->model_globals->select_where('work_history',array('using_resume_id'=> $get_resumId));
        $data['language'] = $this->model_globals->select_where('job_seeker_proficiency_language',array('using_resume_id'=> $get_resumId));
        $data['otherInfo'] = $this->model_globals->select_where('resume',array('using_job_seeker_id'=> $seekerId));

        $this->form_validation->set_rules('school_name','Schoool name','required');
        $this->form_validation->set_rules('education_level','Education Level','required');
        $this->form_validation->set_rules('program_name','Program name','required');
        $this->form_validation->set_rules('start_edu_date','Date attended','required');
        $this->form_validation->set_rules('end_edu_date','Date graduated','required');

        $this->form_validation->set_rules('job_title','Job title','required');
        $this->form_validation->set_rules('copany_name','Company name','required');
        $this->form_validation->set_rules('employer_name','Employer name','required');
        $this->form_validation->set_rules('employer_address','Employer address','required');
        $this->form_validation->set_rules('job_duty','Job duty','required');
        $this->form_validation->set_rules('start_exp_date','Start date of experience','required');
        $this->form_validation->set_rules('end_pex_date','End date of experience','required');

        $this->form_validation->set_rules('navtive_language','Native language','required');
        $this->form_validation->set_rules('language','Language','required');
        $this->form_validation->set_rules('level','Language level','required');

        $this->form_validation->set_rules('skill','Skill name','required');
        $this->form_validation->set_rules('interest','Interest','required');
        $this->form_validation->set_rules('reference','Reference','required');

        if($this->form_validation->run()!=TRUE){
            $data['username']=$this->get_name_jobseeker();
            $data['locations']=$this->model_globals->select_all('job_locations');
            $categories=$this->model_globals->select_all('job_categories')->result();
            $option = $this->get_job_category_options($categories);
            $data['laguags'] = $this->model_globals->select_all('languages',150);
            $data['language_level']= $this->model_globals->select_all('proficiency_languages',150);
            $data['education_level']= $this->model_globals->select_all('education_levels',30);

            $this->load->view(F_MASTER."index",$data);
        }else{
            $this->model_globals->delete('job_seeker_educations',array('using_resume_id'=>$get_resumId));
            $this->model_globals->delete('work_history',array('using_resume_id'=>$get_resumId));
            $this->model_globals->delete('job_seeker_proficiency_language',array('using_resume_id'=>$get_resumId));

            $data_native_language = array(
                'using_job_seeker_id' => $this -> session -> userdata('jobseeker_active_access'),
                'skill' => $this->input->post('skill',TRUE),
                'native_language_using_language_id' =>  $this->input->post('navtive_language',TRUE),
                'interesting' => $this->input->post('interest',TRUE),
                'video_resume' => "",
                'reference' => $this->input->post('reference',TRUE),
                'created_date' => null,
                'status' => TRUE
            );
            $result = FALSE;
            $this->model_globals->update('resume',$data_native_language,array('resume_id'=>$get_resumId)); /*Insert native language & return last resume id*/
            $resumId = $this->model_globals->select_string('resume','resume_id',array('resume_id'=>$get_resumId));
            $language = $this->input->post('language',TRUE);
            $language_level = $this->input->post('level',TRUE);
            for($i=0; $i<count($language); $i++){
                $result = TRUE;
                /*Insert proficiency languae as an array*/
                $this->model_globals->insert('job_seeker_proficiency_language',array('using_resume_id'=>$resumId,'using_language_id'=>$language[$i],'using_proficiency_language_id'=>$language_level[$i],'created_date'=>NULL,'status'=>TRUE));

            }
            $education_level = $this->input->post('education_level',TRUE);
            $school_name = $this->input->post('school_name',TRUE) ;
            $program_name = $this->input->post('program_name',TRUE);
		
            $edu_start_date = $this->input->post('start_edu_date',TRUE);
			$edu_end_date = $this->input->post('end_edu_date',TRUE);
			
			
            for($i=0; $i<count($education_level); $i++){
            	
			$date_start_input = explode("/", $edu_start_date[$i]);
			$date_start_db = $date_start_input[2] . "-" . $date_start_input[1] . "-" . $date_start_input[0];
			
			$date_end_input = explode("/", $edu_end_date[$i]);
			$date_end_db = $date_end_input[2] . "-" . $date_end_input[1] . "-" . $date_end_input[0];

                $result = TRUE;
                /*Insert seeker's education as an array'*/
                $this->model_globals->insert('job_seeker_educations',array('using_resume_id'=>$resumId,'using_education_level_id'=>$education_level[$i],'school_name'=>$school_name[$i],'program_name'=>$program_name[$i],'date_attended'=>$date_start_db,'date_gratuated'=>$date_end_db,'created_date'=>NULL,'status'=>TRUE));
                          
            }
            $job_title = $this->input->post('job_title',TRUE);
            $company_name = $this->input->post('copany_name',TRUE);
            $employer_name = $this->input->post('employer_name',TRUE);
            $employer_address = $this->input->post('employer_address',TRUE);
            $job_duty = $this->input->post('job_duty',TRUE);
            $start_expDate = $this->input->post('start_exp_date',TRUE);
            $end_expDate = $this->input->post('end_pex_date',TRUE);
            for($i=0; $i<count($job_title); $i++){
                $result = TRUE;
                /*Insert seeker's work history as an array*/
                $this->model_globals->insert('work_history',array('using_resume_id'=>$resumId,'job_title'=>$job_title[$i],'company_name'=>$company_name[$i],'employer_name'=>$employer_name[$i],'employer_address'=>$employer_address[$i],'job_duties'=>$job_duty[$i],'start_date_end_date'=>$start_expDate[$i].'-'.$end_expDate[$i],'created_date'=>NULL,'status'=>TRUE));
            }
            if($result != FALSE){
                $this->session->set_flashdata('success','Your resume has been successful created!');
            }else{
                $this->session->set_flashdata('error','Your resume has not been successful created!');
            }
           redirect('jobseekers/resume');

        }
    }
	public function application_history()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Application History";
		$data['username']=$this->get_name_jobseeker();
		$data['job_applications']=$this->model_globals->select_join_get_only_fields('job_seeker_applied_jobs', $arr_fields = array('job_seeker_applied_job_id','job_seekers.first_name','job_seekers.last_name','job_ads.title','apply_status','apply_date','reason','job_keys'),array('job_seekers' => array('using_job_seeker_id' => 'job_seeker_id'),'job_ads' => array('using_job_ads_id' => 'job_ads_id')), $join_type = 'INNER', $arr_where = array('job_seeker_applied_jobs.using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')), $limit = NULL,$sort="job_seeker_applied_job_id DESC");
		$this->load->view(F_MASTER."index",$data);
	}
	public function change_picture()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Change Picture Profile";
		if ($this->input->post('checking')) {
			
            $config['upload_path'] = './Templates/F-Template/F-IMAGES/jobseeker_profile/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '30000';
            $this->load->library('upload', $config);

            if (!$this->upload->do_upload()) {
            	$data['errors']= array('error' => $this->upload->display_errors());
                $this->load->view(F_MASTER."index",$data);
            } else {

                $data = $this->upload->data();
                $image_name = $data['file_name'];

                $img_width = $data['image_width'];
                $img_height = $data ['image_height'];

                $scale_height = 120 / $img_height;
                $fit_width = $img_width * $scale_height;
                $fit_height = $img_height * $scale_height;

                $config['image_library'] = 'gd2';
                $config['source_image'] = "./Templates/F-Template/F-IMAGES/jobseeker_profile/$image_name";
                $config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = $fit_width;
                $config['height'] = $fit_height;

                $this->load->library('image_lib', $config);

                $this->image_lib->resize();
                if (!$this->image_lib->resize()) {
                    echo $this->image_lib->display_errors();
                    exit;
                }

                //$logo_path = "templates/back_end/images/";
                $rs = $this->model_globals->update('job_seekers',array('picture'=>$image_name),array('job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
                if ($rs) {
                    $this->session->set_flashdata('success', 'Your picture profile was modified success .');
                    redirect('jobseekers/change-picture');
                } else {
                    $this->session->set_flashdata('error', 'Your picture profile was modified fail !');
                    redirect('jobseekers/change-picture');
                }
            }
        }else{
        	 $this->load->view(F_MASTER."index",$data);
        }
	}
    
	public function resume_editor()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Create Resume Text Editor";
		$this -> form_validation -> set_rules('editor1', 'Resume', 'required');
		if ($this -> form_validation -> run() == FALSE) {
			$this->load->view(F_MASTER."index",$data);
		}else{
			$user_resume_editor=$this->input->post('editor1');
			$is_inserted=$this->model_globals->insert('resume_text_editors',array('using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access'),'text_resume'=>$user_resume_editor,'status'=>1,'created_date'=>null));
			if($is_inserted){
				$this -> session -> set_flashdata('success', 'You have been created your resume online editor success !.');
				redirect('jobseekers/resume');
			}else{
				$this -> session -> set_flashdata('error', 'You have been created your resume online editor fail !.');
				redirect('jobseekers/resume');
			}
		}
		
		
	}
	public function modify_resume_editor()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Modify Your Resume Editor";
		$this -> form_validation -> set_rules('editor1', 'Resume', 'required');
		if ($this -> form_validation -> run() == FALSE) {
			$data['resume_editor_info']=$this->model_globals->select_where("resume_text_editors",array('using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
			$this->load->view(F_MASTER."index",$data);
		}else{
			$user_resume_editor=$this->input->post('editor1');
			$is_updated=$this->model_globals->update('resume_text_editors',array('text_resume'=>$user_resume_editor),array('using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
			if($is_updated){
				$this -> session -> set_flashdata('success', 'You have been modified your resume online editor success !.');
				redirect('jobseekers/resume');
			}else{
				$this -> session -> set_flashdata('error', 'You have been modified your resume online editor fail !.');
				redirect('jobseekers/resume');
			}
		}
		
		
	}
	public function view_profile()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="View Profile";
		$data['user_info']=$this->model_globals->select_where("job_seekers",array("job_seeker_id"=>$this -> session -> userdata('jobseeker_active_access')));
		$this->load->view(F_MASTER."index",$data);
	}
	public function video_resume(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		if($this->is_isset_resume($this -> session -> userdata('jobseeker_active_access'))){
            $data['cv'] = TRUE;
        }else{
            $data['cv'] = FALSE;
        }
		$data['title']="Video Resume";
		$data['username']=$this->get_name_jobseeker();
		if ($this->input->post('checking')) {
			
            $config['upload_path'] = './Templates/F-Template/F-VIDEOS/jobseeker_video_resume/';
            $config['allowed_types'] = 'avi|flv|wmv|mp3';
            $config['max_size'] = '10240';
            $this->load->library('upload', $config);

            if (!$this->upload->do_upload()) {
            	$data['errors']= array('error' => $this->upload->display_errors());
                $this->load->view(F_MASTER."index",$data);
            } else {

                $data = $this->upload->data();
                $video_name = $data['file_name'];

                //$logo_path = "templates/back_end/images/";
                $rs = $this->model_globals->update('job_seekers',array('video_resume'=>$video_name),array('job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
                if ($rs) {
                    $this->session->set_flashdata('success', 'Your video resume was modified success .');
                    redirect('jobseekers/video-resume');
                } else {
                    $this->session->set_flashdata('error', 'Your video resume was modified fail !');
                     redirect('jobseekers/video-resume');
                }
            }
        }else{
        	 $this->load->view(F_MASTER."index",$data);
        }
	}
	public function logout()
	{
		$this -> session -> sess_destroy();
		redirect('jobseekers/login');
		//$this->load->view(F_MASTER."index",$data);
	}
	public function get_name_jobseeker(){
		$seeker_info=$this->model_globals->select_where('job_seekers',array("job_seeker_id"=>$this -> session -> userdata('jobseeker_active_access')));
		return ucfirst($seeker_info->row()->first_name).".".strtoupper($seeker_info->row()->last_name);
	}

	function brief_data($job_seeker_id){
		$this->model_globals->insert('job_seekers_job_preference',array('using_job_seeker_id'=>$job_seeker_id,'briefly_description'=>"",'status'=>1,'created_date'=>null));
		
		$locations=$this->model_globals->select_all('job_locations');
		foreach($locations->result() as $location){
			$loc_id=$location->job_location_id;
			$this->model_globals->insert('job_preference_locations',array('using_job_seeker_id'=>$job_seeker_id,'using_location_id'=>$loc_id,'is_prefered'=>'NO','status'=>1,'created_date'=>null));
		}
		
		$categories=$this->model_globals->select_all('job_categories')->result();
			$option="";
			foreach($categories as  $category){
				$category_id=$category->job_category_id;
				$name=$category->name;
				$option.='<optgroup label="'.$name.'">';
				$sub_categories=$this->model_globals->select_where('job_sub_categories',array('using_job_category_id'=>$category_id))->result();
				foreach($sub_categories as $sub_category){
					$sub_category_id=$sub_category->job_sub_category_id;
					$sub_name=$sub_category->name;
					$this->model_globals->insert('job_preference_categories',array('using_job_seeker_id'=>$job_seeker_id,'using_job_category_id'=>$category_id,'using_job_sub_category_id'=>$sub_category_id,'is_prefered'=>'NO','status'=>1,'created_date'=>null));
				}
				
			}
	}
	
	
	//---------------------------------------Send mail---------------------------------------//
	
	public function send_mail_to_jobseeker_activation_account($activation_key,$email){
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$message="Dear $email,<br />
						<p>You or someone with your email id signed up at jobpending.com website,Your new account is almost ready, but before you can login.You need to confirm your email id by visiting the link below:<br />
						<a href='".base_url()."jobseekers/activation-account/".$activation_key."'>".base_url()."jobseekers/activation-account/".$activation_key."</a></p>
						
						<p>One you have visited the verification URL, your account will be activated.</p>
						
						</p>if you have any problems or questions, Please reply to this email.</p>
						
						</p>Best regard,</p>
						</p>Jobpending team</p>
						<p>================================================</p>
						<p>
							Email: info@jobpending.com<br />
							Tel: 015 59 17 37 / 069 90 91 69<br />
							Address: Nº 13, Street 420, Songkat Boeng Trabek,Khan Chamkar Morn, Phnom   Penh, Cambodia. <br />
							Facebook: <a href='https://www.facebook.com/pages/Jobpending/267500723459814'>https://www.facebook.com/pages/Jobpending/267500723459814</a>
						</p>";

		$this->email->from('info@jobpending.com', 'info@jobpending.com');
		$this->email->to($email); 
		$this->email->cc('jobpending.info@gmail.com'); 
		//$this->email->bcc('them@their-example.com'); 
		
		$this->email->subject('Jobpending activation account');
		$this->email->message($message);	
		
		if($this->email->send()){
			return TRUE;
		}else{
			echo $this->email->print_debugger();
			exit;
			return FALSE;
		}
	}
	public function send_mail_to_jobseeker_reset_password($key_active_password,$email){
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$message="Dear Sir/Madam,<br />

				<p>We've sent this message because you have requested that your jobpending password to be reset.</br />
				To get back into your jobpending accout you will need to create a new password.</p>
				
				<p>-------------------------------------------------------</p>
				<p>Here's how you do that <br />
				1.Click the link below to open a new and secure browser window.<br />
				2.Enter the requested information and follow the instruction to reset your password.</p>
				
				<p>Reset your password now:<br />
				<a href='".base_url()."jobseekers/activation-forgotten-password/".$key_active_password."'>".base_url()."jobseekers/activation-forgotten-password/".$key_active_password."</a></p>
				
				<p>Best regard,</p>
				<p>Jobpending team</p>
				<p>================================================</p>
				<p>
					Email: info@jobpending.com<br />
					Tel: 015 59 17 37 / 069 90 91 69<br />
					Address: Nº 13, Street 420, Songkat Boeng Trabek,Khan Chamkar Morn, Phnom   Penh, Cambodia. <br />
					Facebook: <a href='https://www.facebook.com/pages/Jobpending/267500723459814'>https://www.facebook.com/pages/Jobpending/267500723459814</a>
				</p>";
				
		$this->email->from('info@jobpending.com', 'info@jobpending.com');
		$this->email->to($email); 
		$this->email->cc('jobpending.info@gmail.com'); 
		//$this->email->bcc('them@their-example.com'); 
		
		$this->email->subject('Jobpending reset  account password');
		$this->email->message($message);	
		
		if($this->email->send()){
			return TRUE;
		}else{
			echo $this->email->print_debugger();
			exit;
			return FALSE;
		}
	}
	public function send_mail_to_jobseeker_resend_activate_account($activation_key,$email){
		$this->load->library('email');
		$this->email->set_mailtype("html");
		$message="Dear $email,<br />
						<p>You or someone with your email id signed up at jobpending.com website have requested to resent activate account,Your new account is almost ready, but before you can login. You need to confirm your email id by visiting the link below:<br />
						<a href='".base_url()."jobseekers/activation-account/".$activation_key."'>".base_url()."jobseekers/activation-account/".$activation_key."</a></p>
						
						<p>One you have visited the verification URL, your account will be activated.</p>
						
						<p>if you have any problems or questions, Please reply to this email.</p>
						
						<p>Best regard,</p>
						<p>Jobpending team</p>
						<p>================================================</p>
						<p>
							Email: info@jobpending.com<br />
							Tel: 015 59 17 37 / 069 90 91 69<br />
							Address: Nº 13, Street 420, Songkat Boeng Trabek,Khan Chamkar Morn, Phnom   Penh, Cambodia. <br />
							Facebook: <a href='https://www.facebook.com/pages/Jobpending/267500723459814'>https://www.facebook.com/pages/Jobpending/267500723459814</a>
						</p>";
		$this->email->from('info@jobpending.com', 'info@jobpending.com');
		$this->email->to($email); 
		$this->email->cc('jobpending.info@gmail.com'); 
		//$this->email->bcc('them@their-example.com'); 
		
		$this->email->subject('Jobpending activation account');
		$this->email->message($message);	
		
		if($this->email->send()){
			return TRUE;
		}else{
			echo $this->email->print_debugger();
			exit;
			return FALSE;
		}
	}
	public function job_favorites()
	{
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$data['title']="Job Favorites";
		$data['username']=$this->get_name_jobseeker();
		$this->load->model('F-MODEL/model_job_seekers');
		$data['job_favorites']=$this->model_job_seekers->get_manage_job_favorites();
		$this->load->view(F_MASTER."index",$data);
	}
	public function delete_job_favorite(){
		if (!$this -> session -> userdata('jobseeker_active_access')) {
			redirect("jobseekers/login");
		}
		$job_favorite_id=$this->uri->segment(3);
		$is_deleted=$this->model_globals->delete('job_favorites',array('job_favorite_id'=>$job_favorite_id,'using_job_seeker_id'=>$this->session->userdata('jobseeker_active_access')));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'You have deleted your job favorite successfully !.');
			redirect('jobseekers/job-favorites');
		}else{
			$this -> session -> set_flashdata('error', 'Your have deleted your job favorite fail !.');
			redirect('jobseekers/job-favorites');
		}
	}
	public function get_job_category_options($categories){
		$option='';
		foreach ($categories as $category) {
			$category_id = $category -> job_category_id;
			$name = $category -> name;
			//$option .= '<optgroup label="' . $name . '">';
			$option .= '<option value="' . $category_id . '-0 " style="color:#3C8DBC;">' . $name . '</option>';
			$sub_categories = $this -> model_globals -> select_where('job_sub_categories', array('using_job_category_id' => $category_id)) -> result();
			foreach ($sub_categories as $sub_category) {
				$sub_category_id = $sub_category -> job_sub_category_id;
				$sub_name = $sub_category -> name;
				$option .= '<option value="' . $category_id . '-' . $sub_category_id . '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $sub_name . '</option>';
			}
			//$option .= '</optgroup>';
		}
		return $option;
		
	}
}
